package net.xiaoxiangshop.controller.member;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import net.xiaoxiangshop.Results;
import net.xiaoxiangshop.entity.Business;
import net.xiaoxiangshop.service.MemberService;
import net.xiaoxiangshop.service.SmsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import net.xiaoxiangshop.entity.Member;
import net.xiaoxiangshop.entity.SocialUser;
import net.xiaoxiangshop.security.CurrentUser;
import net.xiaoxiangshop.service.PluginService;
import net.xiaoxiangshop.service.SocialUserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller - 会员登录
 * 
 */
@Controller("memberLoginController")
@RequestMapping("/member/login")
public class LoginController extends BaseController {

	@Value("${security.member_login_success_url}")
	private String memberLoginSuccessUrl;

	@Inject
	private PluginService pluginService;
	@Inject
	private SocialUserService socialUserService;
	@Inject
	private MemberService memberService;
	@Inject
	private SmsService smsService;
	/**
	 * 登录页面
	 */
	@GetMapping
	public String index(Long socialUserId, String uniqueId, @CurrentUser Member currentUser, HttpServletRequest request, ModelMap model) {
		model.addAttribute("memberLoginSuccessUrl", memberLoginSuccessUrl);
		model.addAttribute("loginPlugins", pluginService.getActiveLoginPlugins(request));
		String toLogin=request.getParameter("toLogin");
		if (socialUserId != null && StringUtils.isNotEmpty(uniqueId)) {
			SocialUser socialUser = socialUserService.find(socialUserId);
			if (socialUser == null || socialUser.getUser() != null || !StringUtils.equals(socialUser.getUniqueId(), uniqueId)) {
				return UNPROCESSABLE_ENTITY_VIEW;
			}
			model.addAttribute("socialUserId", socialUserId);
			model.addAttribute("uniqueId", uniqueId);
		}
		if("mobile".equals(toLogin)){//手机验证码校验失败--跳转到用户名密码登录页面
			return "/member/login/login";
		}
		return "/member/login/index";
	}
	/**
	 * business登陆获取短信验证码
	 * @param mobile
	 * @return
	 */
	@GetMapping("/getSmsCode")
	public ResponseEntity<?> getSmsCode(String mobile) {
		//判断手机号是存在
		Boolean exists=memberService.usernameMobileExists(mobile);
		if(!exists){
			smsService.sendSmsCode("1",mobile);
			return responseReslut(mobile,mobile,200,"短信验证已发送");
		}
		//判断手机号是否唯一
//		Boolean unique=memberService.mobileUnique(null,mobile);
		List<Member> memberList=memberService.memberList(mobile);
		if(memberList!=null&&memberList.size()>1){
			return responseReslut(null,null,100,"此手机号存在多个,请使用用户名密码登录");
		}
		smsService.sendSmsCode("1",mobile);
		Member member= memberService.findByMobile(mobile);
		return responseReslut(member.getUsername(),member.getMobile(),200,"短信验证已发送");
	}

	/**
	 * 校验短信验证码
	 * @param mobile
	 * @param smsCode
	 * @return
	 */
	@PostMapping("/checkSmsCode")
	public ResponseEntity<?> checkSmsCode(String mobile, String smsCode) {
		ResponseEntity<?> reslut=smsService.checkSmsCode("1",mobile,smsCode);
		Integer code= reslut.getStatusCodeValue();
		if(code==200){//验证码通过、
			return responseReslut(null,null,200,"短信验证码校验成功");
		}
		return responseReslut(null,null,100,"短信验证码校验失败");
	}

	public ResponseEntity<?> responseReslut(String username,String mobile,Integer code,String message){
		Map<String,Object> responseReslut=new HashMap<>();
		responseReslut.put("code",code);
		responseReslut.put("username",username);
		responseReslut.put("mobile",mobile);
		responseReslut.put("message",message);
		return ResponseEntity.ok(responseReslut);
	}
}